package com.cropdoctor.search;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.cropdoctor.mapper.PostMapper;
import com.cropdoctor.mapper.PublishCourseMapper;
import com.cropdoctor.mapper.VegeFruTableMapper;
import com.cropdoctor.model.doc.PostDoc;
import com.cropdoctor.model.doc.PublishCourseDoc;
import com.cropdoctor.model.doc.VegeFruTableDoc;
import com.cropdoctor.model.dto.VegeFrutableDto;
import com.cropdoctor.model.po.PostPo;
import com.cropdoctor.model.po.PublishCoursePo;
import com.cropdoctor.model.po.VegeFrutablePo;
import com.cropdoctor.util.RedisSearchUtils;
import io.redisearch.AggregationResult;
import io.redisearch.Query;
import io.redisearch.Schema;
import io.redisearch.SearchResult;
import io.redisearch.aggregation.AggregationBuilder;
import io.redisearch.aggregation.SortedField;
import io.redisearch.aggregation.reducers.Reducers;
import io.redisearch.client.Client;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootTest
public class testRedisSearch {
    @Autowired
    VegeFruTableMapper vegeFruTableMapper;

    @Autowired
    @Qualifier("tableSearchClient")
    Client redisSearchClient;
    @Qualifier("tableSearchClient")
    @Autowired
    private Client tableSearchClient;
    @Autowired
    PublishCourseMapper publishCourseMapper;
    @Autowired
    private PostMapper postMapper;

    /**
     * 创建表索引
     */
    @Test
    public void addAllVege() {

        List<VegeFrutablePo> list = vegeFruTableMapper.selectList(null);

        list.forEach(item -> {
            Map<String, Object> fields = new HashMap<>();
            fields.put("id", item.getId());
            fields.put("name", item.getName());
            fields.put("st", item.getSt());
            fields.put("mt", item.getMt());
            fields.put("alias", item.getAlias());
            fields.put("des", item.getDes());
            fields.put("url", item.getUrl());
            fields.put("suggestion", item.getSuggestion());
            fields.put("family", item.getFamily());
            fields.put("light", item.getLight());
            fields.put("soilTypes", item.getSoilTypes());

            fields.put("soilPh", item.getSoilPh());
            fields.put("reproductivePeriod", item.getReproductivePeriod());

            redisSearchClient.addDocument("table:"+item.getId().toString(),fields);
        });
    }

    @Test
    public void addAllCourse()
    {
        List<PublishCoursePo> list = publishCourseMapper.selectList(null);

        list.forEach(item -> {
            PublishCourseDoc publishCourseDoc = new PublishCourseDoc(item);
            Map<String, Object> fields = BeanUtil.beanToMap(publishCourseDoc);

            redisSearchClient.addDocument("course:" + item.getId(), fields);
        });

        List<PostPo> list1 = postMapper.selectList(null);
        list1.forEach(item->{
            PostDoc postDoc = new PostDoc(item);
            Map<String, Object> fields = BeanUtil.beanToMap(postDoc);
            redisSearchClient.addDocument("post:" + item.getId(), fields);
        });
    }
    
    @Test
    public void testSearch()
    {
        Query q = new Query("西瓜 @mt:{水果}");

        // actual search
        SearchResult res = tableSearchClient.search(q);
        System.out.println(res);
    }



}
